package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.SysMsgSetting;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.SysMsgSettingBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author hejj
 */
@RequestMapping("/base")
@Controller
public class SysMsgSettingAction {
    private static final long serialVersionUID = 1L;

    @Resource
    private SysMsgSettingBiz sysMsgSettingBiz;

    /**
     * public String show() { try { page = initPage(TSysMsgSettingBiz.findEntityCount(filters));
     * Order order = new Order(); order.add(sidx, sord); TSysMsgSettings = TSysMsgSettingBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order); }
     * catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
     **/

    @Resource
    public void setTSysMsgSettingBiz(SysMsgSettingBiz tSysMsgSettingBiz) {
        this.sysMsgSettingBiz = tSysMsgSettingBiz;
    }

    @RequestMapping("msgSetting-main")
    public String main(Model model) {
        model.addAttribute("title", "通知设定管理");
        model.addAttribute("left", "msgSetting-tree");
        model.addAttribute("right", "");
//		title = "通知设定管理";
//		left = "msgSetting-tree.do";
        // right = "mod-pms.do";
//		right = "";
        return WebPathConstants.FRAME_WEB_PATH;
    }

    @RequestMapping("/msgSetting-tree")
    public String tree() {
        return "/gtimp/base/msgSetting/msgSetting-tree";
    }

    @RequestMapping("/msgSetting-find")
    public String find() {
        return "/gtimp/base/msgSetting/msgSetting-list";
    }

    @RequestMapping("/msgSetting-listMsgSetting")
    @ResponseBody
    public Page<SysMsgSetting> listMsgSetting(Jqgrid jqgrid, User user, SysMsgSetting sysMsgSetting) {
        try {
            if (StringUtils.isNotEmpty(sysMsgSetting.getProcess().getBizType())) {
//                if (filters == null) {
//                    filters = new Filter();
//                    filters.setGroupOp(FilterGroupOption.AND);
//                }
                SearchRule searchRule = new SearchRule("process.bizType", SearchRuleOption.eq, sysMsgSetting
                        .getProcess().getBizType());
                jqgrid.addRule(searchRule);
                if (user.isAdmin()) {
                    searchRule = new SearchRule("company", SearchRuleOption.eq, null);
                } else {
                    searchRule = new SearchRule("company.id", SearchRuleOption.eq, user
                            .getCompany().getId());
                }
                jqgrid.addRule(searchRule);
//                page = initPage(sysMsgSettingBiz.findEntityCount(filters));
//                sysMsgSettings = sysMsgSettingBiz
//                        .findByJqgrid(page.getCurrentPageStartNo(), page.getPageSize(), filters, null);
              Page<SysMsgSetting> sysMsgSettings = sysMsgSettingBiz.findByJqgrid(jqgrid);
                return  sysMsgSettings;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/msgSetting-manage")
    public String manage() {
        return "/gtimp/base/msgSetting/msgSetting-manage";
    }

    @RequestMapping("/msgSetting-saveOrUpdateSysMsgSetting")
    @ResponseBody
    public Result saveOrUpdateSysMsgSetting(User user,SysMsgSetting sysMsgSetting) {
        Result msg;
        try {
            if (null != sysMsgSetting) {
                if (user.isAdmin()) {
                    sysMsgSetting.setCompany(null);
                } else {
                    Company company = new Company();
                    company.setId(user.getCompany().getId());
                    sysMsgSetting.setCompany(company);
                }
                sysMsgSetting.setUpdateuser(user.getAccount());
                sysMsgSetting.setUpdatetime(new Date());
                if (StringUtils.isEmpty(sysMsgSetting.getId())) {
                    sysMsgSetting.setId(null);
                } else {
                }
                sysMsgSettingBiz.saveOrupdateEntity(sysMsgSetting);
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    @RequestMapping("/msgSetting-getData")
    public SysMsgSetting getData(SysMsgSetting sysMsgSetting) {
        if (null != sysMsgSetting) {
            if (StringUtils.isNotEmpty(sysMsgSetting.getId())) {
                try {
                    sysMsgSetting = sysMsgSettingBiz.findEntityById(sysMsgSetting.getId());
                    return sysMsgSetting;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }




}
